<?php


abstract class BasePost extends BaseObject  implements Persistent {


	
	protected static $peer;


	
	protected $id;


	
	protected $handle = '';


	
	protected $datetime = 943912800;


	
	protected $published = 1;


	
	protected $comment_allow = 1;

	
	protected $collImages;

	
	protected $lastImageCriteria = null;

	
	protected $collPostI18ns;

	
	protected $lastPostI18nCriteria = null;

	
	protected $collPostTags;

	
	protected $lastPostTagCriteria = null;

	
	protected $collComments;

	
	protected $lastCommentCriteria = null;

	
	protected $alreadyInSave = false;

	
	protected $alreadyInValidation = false;

  
  protected $culture;

	
	public function getId()
	{

		return $this->id;
	}

	
	public function getHandle()
	{

		return $this->handle;
	}

	
	public function getDatetime($format = 'Y-m-d H:i:s')
	{

		if ($this->datetime === null || $this->datetime === '') {
			return null;
		} elseif (!is_int($this->datetime)) {
						$ts = strtotime($this->datetime);
			if ($ts === -1 || $ts === false) { 				throw new PropelException("Unable to parse value of [datetime] as date/time value: " . var_export($this->datetime, true));
			}
		} else {
			$ts = $this->datetime;
		}
		if ($format === null) {
			return $ts;
		} elseif (strpos($format, '%') !== false) {
			return strftime($format, $ts);
		} else {
			return date($format, $ts);
		}
	}

	
	public function getPublished()
	{

		return $this->published;
	}

	
	public function getCommentAllow()
	{

		return $this->comment_allow;
	}

	
	public function setId($v)
	{

						if ($v !== null && !is_int($v) && is_numeric($v)) {
			$v = (int) $v;
		}

		if ($this->id !== $v) {
			$this->id = $v;
			$this->modifiedColumns[] = PostPeer::ID;
		}

	} 
	
	public function setHandle($v)
	{

						if ($v !== null && !is_string($v)) {
			$v = (string) $v; 
		}

		if ($this->handle !== $v || $v === '') {
			$this->handle = $v;
			$this->modifiedColumns[] = PostPeer::HANDLE;
		}

	} 
	
	public function setDatetime($v)
	{

		if ($v !== null && !is_int($v)) {
			$ts = strtotime($v);
			if ($ts === -1 || $ts === false) { 				throw new PropelException("Unable to parse date/time value for [datetime] from input: " . var_export($v, true));
			}
		} else {
			$ts = $v;
		}
		if ($this->datetime !== $ts || $ts === 943912800) {
			$this->datetime = $ts;
			$this->modifiedColumns[] = PostPeer::DATETIME;
		}

	} 
	
	public function setPublished($v)
	{

						if ($v !== null && !is_int($v) && is_numeric($v)) {
			$v = (int) $v;
		}

		if ($this->published !== $v || $v === 1) {
			$this->published = $v;
			$this->modifiedColumns[] = PostPeer::PUBLISHED;
		}

	} 
	
	public function setCommentAllow($v)
	{

						if ($v !== null && !is_int($v) && is_numeric($v)) {
			$v = (int) $v;
		}

		if ($this->comment_allow !== $v || $v === 1) {
			$this->comment_allow = $v;
			$this->modifiedColumns[] = PostPeer::COMMENT_ALLOW;
		}

	} 
	
	public function hydrate(ResultSet $rs, $startcol = 1)
	{
		try {

			$this->id = $rs->getInt($startcol + 0);

			$this->handle = $rs->getString($startcol + 1);

			$this->datetime = $rs->getTimestamp($startcol + 2, null);

			$this->published = $rs->getInt($startcol + 3);

			$this->comment_allow = $rs->getInt($startcol + 4);

			$this->resetModified();

			$this->setNew(false);

						return $startcol + 5; 
		} catch (Exception $e) {
			throw new PropelException("Error populating Post object", $e);
		}
	}

	
	public function delete($con = null)
	{
		if ($this->isDeleted()) {
			throw new PropelException("This object has already been deleted.");
		}

		if ($con === null) {
			$con = Propel::getConnection(PostPeer::DATABASE_NAME);
		}

		try {
			$con->begin();
			PostPeer::doDelete($this, $con);
			$this->setDeleted(true);
			$con->commit();
		} catch (PropelException $e) {
			$con->rollback();
			throw $e;
		}
	}

	
	public function save($con = null)
	{
		if ($this->isDeleted()) {
			throw new PropelException("You cannot save an object that has been deleted.");
		}

		if ($con === null) {
			$con = Propel::getConnection(PostPeer::DATABASE_NAME);
		}

		try {
			$con->begin();
			$affectedRows = $this->doSave($con);
			$con->commit();
			return $affectedRows;
		} catch (PropelException $e) {
			$con->rollback();
			throw $e;
		}
	}

	
	protected function doSave($con)
	{
		$affectedRows = 0; 		if (!$this->alreadyInSave) {
			$this->alreadyInSave = true;


						if ($this->isModified()) {
				if ($this->isNew()) {
					$pk = PostPeer::doInsert($this, $con);
					$affectedRows += 1; 										 										 
					$this->setId($pk);  
					$this->setNew(false);
				} else {
					$affectedRows += PostPeer::doUpdate($this, $con);
				}
				$this->resetModified(); 			}

			if ($this->collImages !== null) {
				foreach($this->collImages as $referrerFK) {
					if (!$referrerFK->isDeleted()) {
						$affectedRows += $referrerFK->save($con);
					}
				}
			}

			if ($this->collPostI18ns !== null) {
				foreach($this->collPostI18ns as $referrerFK) {
					if (!$referrerFK->isDeleted()) {
						$affectedRows += $referrerFK->save($con);
					}
				}
			}

			if ($this->collPostTags !== null) {
				foreach($this->collPostTags as $referrerFK) {
					if (!$referrerFK->isDeleted()) {
						$affectedRows += $referrerFK->save($con);
					}
				}
			}

			if ($this->collComments !== null) {
				foreach($this->collComments as $referrerFK) {
					if (!$referrerFK->isDeleted()) {
						$affectedRows += $referrerFK->save($con);
					}
				}
			}

			$this->alreadyInSave = false;
		}
		return $affectedRows;
	} 
	
	protected $validationFailures = array();

	
	public function getValidationFailures()
	{
		return $this->validationFailures;
	}

	
	public function validate($columns = null)
	{
		$res = $this->doValidate($columns);
		if ($res === true) {
			$this->validationFailures = array();
			return true;
		} else {
			$this->validationFailures = $res;
			return false;
		}
	}

	
	protected function doValidate($columns = null)
	{
		if (!$this->alreadyInValidation) {
			$this->alreadyInValidation = true;
			$retval = null;

			$failureMap = array();


			if (($retval = PostPeer::doValidate($this, $columns)) !== true) {
				$failureMap = array_merge($failureMap, $retval);
			}


				if ($this->collImages !== null) {
					foreach($this->collImages as $referrerFK) {
						if (!$referrerFK->validate($columns)) {
							$failureMap = array_merge($failureMap, $referrerFK->getValidationFailures());
						}
					}
				}

				if ($this->collPostI18ns !== null) {
					foreach($this->collPostI18ns as $referrerFK) {
						if (!$referrerFK->validate($columns)) {
							$failureMap = array_merge($failureMap, $referrerFK->getValidationFailures());
						}
					}
				}

				if ($this->collPostTags !== null) {
					foreach($this->collPostTags as $referrerFK) {
						if (!$referrerFK->validate($columns)) {
							$failureMap = array_merge($failureMap, $referrerFK->getValidationFailures());
						}
					}
				}

				if ($this->collComments !== null) {
					foreach($this->collComments as $referrerFK) {
						if (!$referrerFK->validate($columns)) {
							$failureMap = array_merge($failureMap, $referrerFK->getValidationFailures());
						}
					}
				}


			$this->alreadyInValidation = false;
		}

		return (!empty($failureMap) ? $failureMap : true);
	}

	
	public function getByName($name, $type = BasePeer::TYPE_PHPNAME)
	{
		$pos = PostPeer::translateFieldName($name, $type, BasePeer::TYPE_NUM);
		return $this->getByPosition($pos);
	}

	
	public function getByPosition($pos)
	{
		switch($pos) {
			case 0:
				return $this->getId();
				break;
			case 1:
				return $this->getHandle();
				break;
			case 2:
				return $this->getDatetime();
				break;
			case 3:
				return $this->getPublished();
				break;
			case 4:
				return $this->getCommentAllow();
				break;
			default:
				return null;
				break;
		} 	}

	
	public function toArray($keyType = BasePeer::TYPE_PHPNAME)
	{
		$keys = PostPeer::getFieldNames($keyType);
		$result = array(
			$keys[0] => $this->getId(),
			$keys[1] => $this->getHandle(),
			$keys[2] => $this->getDatetime(),
			$keys[3] => $this->getPublished(),
			$keys[4] => $this->getCommentAllow(),
		);
		return $result;
	}

	
	public function setByName($name, $value, $type = BasePeer::TYPE_PHPNAME)
	{
		$pos = PostPeer::translateFieldName($name, $type, BasePeer::TYPE_NUM);
		return $this->setByPosition($pos, $value);
	}

	
	public function setByPosition($pos, $value)
	{
		switch($pos) {
			case 0:
				$this->setId($value);
				break;
			case 1:
				$this->setHandle($value);
				break;
			case 2:
				$this->setDatetime($value);
				break;
			case 3:
				$this->setPublished($value);
				break;
			case 4:
				$this->setCommentAllow($value);
				break;
		} 	}

	
	public function fromArray($arr, $keyType = BasePeer::TYPE_PHPNAME)
	{
		$keys = PostPeer::getFieldNames($keyType);

		if (array_key_exists($keys[0], $arr)) $this->setId($arr[$keys[0]]);
		if (array_key_exists($keys[1], $arr)) $this->setHandle($arr[$keys[1]]);
		if (array_key_exists($keys[2], $arr)) $this->setDatetime($arr[$keys[2]]);
		if (array_key_exists($keys[3], $arr)) $this->setPublished($arr[$keys[3]]);
		if (array_key_exists($keys[4], $arr)) $this->setCommentAllow($arr[$keys[4]]);
	}

	
	public function buildCriteria()
	{
		$criteria = new Criteria(PostPeer::DATABASE_NAME);

		if ($this->isColumnModified(PostPeer::ID)) $criteria->add(PostPeer::ID, $this->id);
		if ($this->isColumnModified(PostPeer::HANDLE)) $criteria->add(PostPeer::HANDLE, $this->handle);
		if ($this->isColumnModified(PostPeer::DATETIME)) $criteria->add(PostPeer::DATETIME, $this->datetime);
		if ($this->isColumnModified(PostPeer::PUBLISHED)) $criteria->add(PostPeer::PUBLISHED, $this->published);
		if ($this->isColumnModified(PostPeer::COMMENT_ALLOW)) $criteria->add(PostPeer::COMMENT_ALLOW, $this->comment_allow);

		return $criteria;
	}

	
	public function buildPkeyCriteria()
	{
		$criteria = new Criteria(PostPeer::DATABASE_NAME);

		$criteria->add(PostPeer::ID, $this->id);

		return $criteria;
	}

	
	public function getPrimaryKey()
	{
		return $this->getId();
	}

	
	public function setPrimaryKey($key)
	{
		$this->setId($key);
	}

	
	public function copyInto($copyObj, $deepCopy = false)
	{

		$copyObj->setHandle($this->handle);

		$copyObj->setDatetime($this->datetime);

		$copyObj->setPublished($this->published);

		$copyObj->setCommentAllow($this->comment_allow);


		if ($deepCopy) {
									$copyObj->setNew(false);

			foreach($this->getImages() as $relObj) {
				$copyObj->addImage($relObj->copy($deepCopy));
			}

			foreach($this->getPostI18ns() as $relObj) {
				$copyObj->addPostI18n($relObj->copy($deepCopy));
			}

			foreach($this->getPostTags() as $relObj) {
				$copyObj->addPostTag($relObj->copy($deepCopy));
			}

			foreach($this->getComments() as $relObj) {
				$copyObj->addComment($relObj->copy($deepCopy));
			}

		} 

		$copyObj->setNew(true);

		$copyObj->setId(NULL); 
	}

	
	public function copy($deepCopy = false)
	{
				$clazz = get_class($this);
		$copyObj = new $clazz();
		$this->copyInto($copyObj, $deepCopy);
		return $copyObj;
	}

	
	public function getPeer()
	{
		if (self::$peer === null) {
			self::$peer = new PostPeer();
		}
		return self::$peer;
	}

	
	public function initImages()
	{
		if ($this->collImages === null) {
			$this->collImages = array();
		}
	}

	
	public function getImages($criteria = null, $con = null)
	{
				include_once 'lib/model/om/BaseImagePeer.php';
		if ($criteria === null) {
			$criteria = new Criteria();
		}
		elseif ($criteria instanceof Criteria)
		{
			$criteria = clone $criteria;
		}

		if ($this->collImages === null) {
			if ($this->isNew()) {
			   $this->collImages = array();
			} else {

				$criteria->add(ImagePeer::POST_ID, $this->getId());

				ImagePeer::addSelectColumns($criteria);
				$this->collImages = ImagePeer::doSelect($criteria, $con);
			}
		} else {
						if (!$this->isNew()) {
												

				$criteria->add(ImagePeer::POST_ID, $this->getId());

				ImagePeer::addSelectColumns($criteria);
				if (!isset($this->lastImageCriteria) || !$this->lastImageCriteria->equals($criteria)) {
					$this->collImages = ImagePeer::doSelect($criteria, $con);
				}
			}
		}
		$this->lastImageCriteria = $criteria;
		return $this->collImages;
	}

	
	public function countImages($criteria = null, $distinct = false, $con = null)
	{
				include_once 'lib/model/om/BaseImagePeer.php';
		if ($criteria === null) {
			$criteria = new Criteria();
		}
		elseif ($criteria instanceof Criteria)
		{
			$criteria = clone $criteria;
		}

		$criteria->add(ImagePeer::POST_ID, $this->getId());

		return ImagePeer::doCount($criteria, $distinct, $con);
	}

	
	public function addImage(Image $l)
	{
		$this->collImages[] = $l;
		$l->setPost($this);
	}

	
	public function initPostI18ns()
	{
		if ($this->collPostI18ns === null) {
			$this->collPostI18ns = array();
		}
	}

	
	public function getPostI18ns($criteria = null, $con = null)
	{
				include_once 'lib/model/om/BasePostI18nPeer.php';
		if ($criteria === null) {
			$criteria = new Criteria();
		}
		elseif ($criteria instanceof Criteria)
		{
			$criteria = clone $criteria;
		}

		if ($this->collPostI18ns === null) {
			if ($this->isNew()) {
			   $this->collPostI18ns = array();
			} else {

				$criteria->add(PostI18nPeer::ID, $this->getId());

				PostI18nPeer::addSelectColumns($criteria);
				$this->collPostI18ns = PostI18nPeer::doSelect($criteria, $con);
			}
		} else {
						if (!$this->isNew()) {
												

				$criteria->add(PostI18nPeer::ID, $this->getId());

				PostI18nPeer::addSelectColumns($criteria);
				if (!isset($this->lastPostI18nCriteria) || !$this->lastPostI18nCriteria->equals($criteria)) {
					$this->collPostI18ns = PostI18nPeer::doSelect($criteria, $con);
				}
			}
		}
		$this->lastPostI18nCriteria = $criteria;
		return $this->collPostI18ns;
	}

	
	public function countPostI18ns($criteria = null, $distinct = false, $con = null)
	{
				include_once 'lib/model/om/BasePostI18nPeer.php';
		if ($criteria === null) {
			$criteria = new Criteria();
		}
		elseif ($criteria instanceof Criteria)
		{
			$criteria = clone $criteria;
		}

		$criteria->add(PostI18nPeer::ID, $this->getId());

		return PostI18nPeer::doCount($criteria, $distinct, $con);
	}

	
	public function addPostI18n(PostI18n $l)
	{
		$this->collPostI18ns[] = $l;
		$l->setPost($this);
	}

	
	public function initPostTags()
	{
		if ($this->collPostTags === null) {
			$this->collPostTags = array();
		}
	}

	
	public function getPostTags($criteria = null, $con = null)
	{
				include_once 'lib/model/om/BasePostTagPeer.php';
		if ($criteria === null) {
			$criteria = new Criteria();
		}
		elseif ($criteria instanceof Criteria)
		{
			$criteria = clone $criteria;
		}

		if ($this->collPostTags === null) {
			if ($this->isNew()) {
			   $this->collPostTags = array();
			} else {

				$criteria->add(PostTagPeer::POST_ID, $this->getId());

				PostTagPeer::addSelectColumns($criteria);
				$this->collPostTags = PostTagPeer::doSelect($criteria, $con);
			}
		} else {
						if (!$this->isNew()) {
												

				$criteria->add(PostTagPeer::POST_ID, $this->getId());

				PostTagPeer::addSelectColumns($criteria);
				if (!isset($this->lastPostTagCriteria) || !$this->lastPostTagCriteria->equals($criteria)) {
					$this->collPostTags = PostTagPeer::doSelect($criteria, $con);
				}
			}
		}
		$this->lastPostTagCriteria = $criteria;
		return $this->collPostTags;
	}

	
	public function countPostTags($criteria = null, $distinct = false, $con = null)
	{
				include_once 'lib/model/om/BasePostTagPeer.php';
		if ($criteria === null) {
			$criteria = new Criteria();
		}
		elseif ($criteria instanceof Criteria)
		{
			$criteria = clone $criteria;
		}

		$criteria->add(PostTagPeer::POST_ID, $this->getId());

		return PostTagPeer::doCount($criteria, $distinct, $con);
	}

	
	public function addPostTag(PostTag $l)
	{
		$this->collPostTags[] = $l;
		$l->setPost($this);
	}


	
	public function getPostTagsJoinTag($criteria = null, $con = null)
	{
				include_once 'lib/model/om/BasePostTagPeer.php';
		if ($criteria === null) {
			$criteria = new Criteria();
		}
		elseif ($criteria instanceof Criteria)
		{
			$criteria = clone $criteria;
		}

		if ($this->collPostTags === null) {
			if ($this->isNew()) {
				$this->collPostTags = array();
			} else {

				$criteria->add(PostTagPeer::POST_ID, $this->getId());

				$this->collPostTags = PostTagPeer::doSelectJoinTag($criteria, $con);
			}
		} else {
									
			$criteria->add(PostTagPeer::POST_ID, $this->getId());

			if (!isset($this->lastPostTagCriteria) || !$this->lastPostTagCriteria->equals($criteria)) {
				$this->collPostTags = PostTagPeer::doSelectJoinTag($criteria, $con);
			}
		}
		$this->lastPostTagCriteria = $criteria;

		return $this->collPostTags;
	}

	
	public function initComments()
	{
		if ($this->collComments === null) {
			$this->collComments = array();
		}
	}

	
	public function getComments($criteria = null, $con = null)
	{
				include_once 'lib/model/om/BaseCommentPeer.php';
		if ($criteria === null) {
			$criteria = new Criteria();
		}
		elseif ($criteria instanceof Criteria)
		{
			$criteria = clone $criteria;
		}

		if ($this->collComments === null) {
			if ($this->isNew()) {
			   $this->collComments = array();
			} else {

				$criteria->add(CommentPeer::POST_ID, $this->getId());

				CommentPeer::addSelectColumns($criteria);
				$this->collComments = CommentPeer::doSelect($criteria, $con);
			}
		} else {
						if (!$this->isNew()) {
												

				$criteria->add(CommentPeer::POST_ID, $this->getId());

				CommentPeer::addSelectColumns($criteria);
				if (!isset($this->lastCommentCriteria) || !$this->lastCommentCriteria->equals($criteria)) {
					$this->collComments = CommentPeer::doSelect($criteria, $con);
				}
			}
		}
		$this->lastCommentCriteria = $criteria;
		return $this->collComments;
	}

	
	public function countComments($criteria = null, $distinct = false, $con = null)
	{
				include_once 'lib/model/om/BaseCommentPeer.php';
		if ($criteria === null) {
			$criteria = new Criteria();
		}
		elseif ($criteria instanceof Criteria)
		{
			$criteria = clone $criteria;
		}

		$criteria->add(CommentPeer::POST_ID, $this->getId());

		return CommentPeer::doCount($criteria, $distinct, $con);
	}

	
	public function addComment(Comment $l)
	{
		$this->collComments[] = $l;
		$l->setPost($this);
	}


	
	public function getCommentsJoinUser($criteria = null, $con = null)
	{
				include_once 'lib/model/om/BaseCommentPeer.php';
		if ($criteria === null) {
			$criteria = new Criteria();
		}
		elseif ($criteria instanceof Criteria)
		{
			$criteria = clone $criteria;
		}

		if ($this->collComments === null) {
			if ($this->isNew()) {
				$this->collComments = array();
			} else {

				$criteria->add(CommentPeer::POST_ID, $this->getId());

				$this->collComments = CommentPeer::doSelectJoinUser($criteria, $con);
			}
		} else {
									
			$criteria->add(CommentPeer::POST_ID, $this->getId());

			if (!isset($this->lastCommentCriteria) || !$this->lastCommentCriteria->equals($criteria)) {
				$this->collComments = CommentPeer::doSelectJoinUser($criteria, $con);
			}
		}
		$this->lastCommentCriteria = $criteria;

		return $this->collComments;
	}

  public function getCulture()
  {
    return $this->culture;
  }

  public function setCulture($culture)
  {
    $this->culture = $culture;
  }

  public function getTitle()
  {
    $obj = $this->getCurrentPostI18n();

    return ($obj ? $obj->getTitle() : null);
  }

  public function setTitle($value)
  {
    $this->getCurrentPostI18n()->setTitle($value);
  }

  public function getContent()
  {
    $obj = $this->getCurrentPostI18n();

    return ($obj ? $obj->getContent() : null);
  }

  public function setContent($value)
  {
    $this->getCurrentPostI18n()->setContent($value);
  }

  protected $current_i18n = array();

  public function getCurrentPostI18n()
  {
    if (!isset($this->current_i18n[$this->culture]))
    {
      $obj = PostI18nPeer::retrieveByPK($this->getId(), $this->culture);
      if ($obj)
      {
        $this->setPostI18nForCulture($obj, $this->culture);
      }
      else
      {
        $this->setPostI18nForCulture(new PostI18n(), $this->culture);
        $this->current_i18n[$this->culture]->setCulture($this->culture);
      }
    }

    return $this->current_i18n[$this->culture];
  }

  public function setPostI18nForCulture($object, $culture)
  {
    $this->current_i18n[$culture] = $object;
    $this->addPostI18n($object);
  }

} 